package cn.ex.ymcc.service.impl;

import cn.ex.ymcc.domain.Systemdictionary;
import cn.ex.ymcc.domain.Systemdictionaryitem;
import cn.ex.ymcc.mapper.SystemdictionaryitemMapper;
import cn.ex.ymcc.vo.SysDicVo;
import cn.ex.ymcc.result.JSONResult;
import cn.ex.ymcc.service.ISysDicService;
import cn.ex.ymcc.service.ISystemdictionaryService;
import cn.ex.ymcc.service.ISystemdictionaryitemService;
import cn.ex.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: ZeWangmu
 * @Date: 2022/08/16/15:56
 * @Description:
 */
@Service
public class SysDicServiceImpl implements ISysDicService {
    @Autowired
    private ISystemdictionaryService systemdictionaryService;

    @Autowired
    private ISystemdictionaryitemService systemdictionaryitemService;



    @Override
    public JSONResult initiSysDic() {
        List<Systemdictionary> sysList = systemdictionaryService.selectList(null);
        AssertUtil.isNotNull(sysList,"没有呀");
        //传承map结构
        Map<Long, Systemdictionary> sysMap = sysList.stream().collect(Collectors.toMap(Systemdictionary::getId, sys -> sys));
        //根据数据字典的id查询数据字典明细
        List<Long> ids = sysList.stream().map(sys -> sys.getId()).collect(Collectors.toList());

        List<Systemdictionaryitem> systemList = systemdictionaryitemService.selectList(null);
        //封装数据
        List<SysDicVo> resultList = new ArrayList<>();
        systemList.stream().forEach(system -> {
            Systemdictionary sys = sysMap.get(system.getParentId());
            if(sys != null) {
                SysDicVo vo = null;
                if(sys.getId() == system.getParentId()) {
                    vo.getSystemdictionaryitems().add(system);
                    vo.setSystemdictionary(sys);
                    resultList.add(vo);
                }
            }

        });
        return JSONResult.success(resultList);




    }
}
